home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Containrs / sa / a_list < prev    next >
Text File  |  1996-04-09  |  5KB  |  181 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- a_list.sa: Array based list
  3. -- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
  4. -- Copyright (C) 1995, International Computer Science Institute
  5. -- $Id: a_list.sa,v 1.6 1996/04/09 10:04:31 borisv Exp $
  6. --
  7. -- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
  8. -- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
  9. -- LICENSE contained in the file: Sather/Doc/License of the
  10. -- Sather distribution. The license is also available from ICSI,
  11. -- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
  12. -------------------------------------------------------------------
  13. class LIST{ETP} < $LIST{ETP} is
  14.    include A_LIST{ETP};
  15. end;
  16. -------------------------------------------------------------------
  17. class A_LIST{ETP} < $LIST{ETP} is
  18.    -- A safe wrapper for FLIST{ETP}
  19.    
  20.    private attr arr: FLIST{ETP};
  21.  
  22.    --              ------ Initialization/Duplication ------
  23.    create: SAME is
  24.       res ::= new;
  25.       res.arr := #FLIST{ETP}(5);
  26.       return res;
  27.    end;
  28.    
  29.    create_sized(n: INT): SAME post result.size = n is
  30.       -- Create a list with "n" empty locations
  31.       if n = 0 then return  #SAME;
  32.       else
  33.      res ::= new;
  34.      res.arr := FLIST{ETP}::create_empty_sized(n);
  35.      return res;
  36.       end;
  37.    end; 
  38.  
  39.    create_from(a: ARRAY{ETP}): SAME is
  40.       -- Create an array which contains the elements of "a"
  41.       return create(a) 
  42.    end;
  43.  
  44.    create(a: $ELT{ETP}): SAME is
  45.       -- Convert "a" into a list
  46.       res: SAME := #;
  47.       loop res.append(a.elt!) end;
  48.       return res;
  49.    end;
  50.    
  51.    resize(n: INT) pre ~void(self) is
  52.       -- Allocate a new array and copy whatever will fit of the
  53.       -- old portion.  Changes the actual size
  54.       resarr::=FLIST{ETP}::create_empty_sized(n);
  55.       i ::= 0; loop until!(i = n); resarr[i] := elt!;  i := i + 1; end;
  56.       arr := resarr;
  57.    end;
  58.  
  59.    copy:SAME is
  60.       -- A copy of self.
  61.       res: SAME := create_sized(size);
  62.       loop i ::= size.times!;
  63.      res[i] := [i];
  64.       end;
  65.       return res;
  66.    end;
  67.  
  68.    copy_from(src:$ELT{ETP}) is
  69.       -- Copy as many elements from `src' to self as will fit.
  70.       loop set!(src.elt!) end;
  71.    end;
  72.    
  73.    --              ------ Insertion/Removal ---------------
  74.    remove_index(i: INT) is arr.delete_ordered(i) end;
  75.    
  76.    insert_after(l:INT,v:ETP) pre  ~void(self) is
  77.       -- Insert v at location i, pushing elements upward
  78.       arr := arr.insert_after(l,v);
  79.    end;
  80.       
  81.    insert_before(l: INT, v: ETP) pre ~void(self) is
  82.       arr := arr.insert_before(l,v);
  83.    end;
  84.    
  85.    insert_all_after(l: INT, e: $CONTAINER{ETP}) pre ~void(self) is
  86.       arr := arr.insert_all_after(l,e);
  87.    end;
  88.    
  89.    insert_all_before(l: INT, e: $CONTAINER{ETP}) pre ~void(self) is
  90.       arr := arr.insert_all_before(l,e);
  91.    end;
  92.  
  93.    append(a:ETP) is
  94.       -- Append element "a", even if a is void
  95.       arr := arr.push(a); 
  96.    end;
  97.    
  98.    append_all(a:$CONTAINER{ETP}) is
  99.       -- Append a into self
  100.       if void(a) then return 
  101.       else loop  arr := arr.push(a.elt!); end;   end;
  102.    end;
  103.  
  104.    clear pre ~void(self) is
  105.       -- Set each array element to void. Set size to zero
  106.       arr.clear
  107.    end;
  108.  
  109.    --              ------ Access/Measurement --------------
  110.    aget(i: INT): ETP pre has_ind(i) is return arr[i] end;
  111.    
  112.    aset(i: INT,v: ETP) pre has_ind(i) is arr[i] := v end;
  113.  
  114.    size: INT is return arr.size end;
  115.    
  116.    --              ------ Queries/Comparison --------------
  117.    has(e: ETP): BOOL is  return arr.has(e) end;
  118.       -- True if array contains "e"
  119.  
  120.    has_ind(i: INT): BOOL is return ~void(self) and 0<=i and i < size end;
  121.    
  122.    has_range(beg,num: INT): BOOL is
  123.       if ~void(self) then
  124.      return 0<=beg and beg<size  and num>= 0 and beg+num<=size;
  125.       else return false end;
  126.    end;
  127.  
  128.    equals(a: $RO_ARR{ETP}):BOOL is return arr.equals(a) end;
  129.    
  130.    --              ------ Cursor --------------------------
  131.    ind!:INT is
  132.       -- Yield the indices of self in order. Self may be void.
  133.       i::=0; sz::=size; loop until!(i>=sz); yield i; i := i+1; end;
  134.    end;
  135.  
  136.    elt!: ETP is
  137.       -- Yield each element of self in order. Self may be void.
  138.       if ~void(self) then
  139.      sz: INT := size; i::=0;
  140.      loop until!(i = sz);  yield arr[i];  i := i + 1; end;
  141.       end 
  142.    end;
  143.    
  144.    set!(val:ETP) is
  145.       -- Set successive elements of self to the values of `val'.       
  146.       sz: INT := size; i::=0;
  147.       loop until!(i = sz); arr[i] := val;  yield; i := i + 1; end;
  148.    end;      
  149.  
  150.    --              ------ Conversion ----------------------
  151.    str: STR is
  152.       -- Prints out a string version of the array of the components 
  153.       -- that are under $STR, and their associated indices
  154.       res ::= #FSTR("{");
  155.       i:INT :=0; asz ::= size; loop until!(i>=asz);
  156.      e ::= [i];
  157.      res := res+",".separate!(elt_str(e,i));
  158.      i := i + 1;
  159.       end;
  160.       res := res +"}";
  161.       return(res.str);
  162.    end;
  163.  
  164.    private elt_str(e: ETP,i: INT): STR is
  165.       typecase e 
  166.       when $STR then return e.str  else return "Unprintable:"+i end;
  167.    end;  
  168.    
  169.    --   subarr(beg,num:INT): LIST{ETP}
  170.    --   -- A new array with `num' entries copied from self 
  171.    --   -- starting at `beg'. Self may not be void.
  172.    --      pre ~void(self) and beg.is_bet(0,size-1)  and num.is_bet(0,size-beg) 
  173.    --   is
  174.    --      r::=create(num);
  175.    --      r.copy_from(beg,num,self);
  176.    --      return r 
  177.    --   end;
  178.  
  179. end;
  180. --=============================================================================
  181.